import {
    MessageBox,
    Message
  } from 'element-ui';
import apiBase from "@/api/apiBase.js";
import { ORDER } from "@/const/orderConst.js";
var baseUrl;
var socket;
var imgPath = "";
var imgs = [];

export default {
    //连接
    connects(identitydata) {
        console.log('我是连接1');
        baseUrl = "ws://127.0.0.1:12345";		  
        openSockets(identitydata);
    },
}

function openSockets(identitydata) {	
    socket = new WebSocket(baseUrl);
    socket.onclose = function()
    {
        console.error("web channel closed");
    };
    socket.onerror = function(error)
    {
        console.error("web channel error: " + error);
    };
    socket.onopen = function()
    {
        new QWebChannel(socket, function(channel) {
            //获取c++注册对象
            window.dialog = channel.objects.dialog;
            //网页关闭函数
            window.onunload = function() {
                dialog.get_actionType("closeSignal");
            }
         
            //二代证阅读
            dialog.get_actionType("singleReadIDCard");
            // document.getElementById("startIDCard").onclick = function() {
            //     dialog.get_actionType("startIDCard");
            // };

            //服务器返回消息
            dialog.sendPrintInfo.connect(function(message) {
                console.log(message);
                if(message.indexOf('start')>=0){
                    Message.success("身份证读卡开启");
                }else if(message.indexOf('IDcardInfo')>=0){
                    Message.success("身份证读卡成功");
                    identitydata(message);
                }
                // Message.success("身份证读卡开启");
                //图片保存后返回路径，根据文件路径增加缩略图
                //字符串格式savePhoto_success:C:/Users/Administrator/Desktop/eloamPhoto/2018.06.25.19.32.31.jpg
            
            });
            //网页加载完成信号
            dialog.html_loaded("one");
        });
    }
}


function connect()
			{
				//socket.close();
				//socket = null;
				var ip = document.getElementById("serverIP").value;
				baseUrl = "ws://" + ip + ":12345";
				var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//正则表达式     
				if(re.test(ip))     
				{     
					if( RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256)
					{
						//alert("IP有效" + baseUrl);
						openSockets();					
					} 					
				} 
				else{
					alert("请输入有效的IP地址" + baseUrl);
				}						
            }
export{identitydata}  
